#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import tempfile

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.pith.packets.meta import Meta
from oriolus.pith.packets.metareader import FileMetaReader, FileMetaReaderError
from oriolus.pith.packets.metawriter import FileMetaWriter, FileMetaWriterError
from unittest import TestCase, TestLoader, TextTestRunner

class TestMeataFileWriter(TestCase):
    def setUp(self):
        self.__reader = FileMetaReader()
        self.__writer = FileMetaWriter()        
        self.__workdir = os.path.join("./testdata/meta")
        self.__meta_file = tempfile.mktemp("", "", self.__workdir)
        
    def test_correct(self):
         meta = Meta("Name",
                    "1.2.3.4",
                    "Description",
                    "[{Name: test_packet_lake, Version: 1.2.0.0}]",
                    "[{Name: test_packet, Version: (,10.0.0.1]}]",
                    "[{Name: fake_packet2, Version: [1.0, 1.2]}]")
         
         try:             
             self.__writer.write(meta, self.__meta_file)
             meta2 = self.__reader.read(self.__meta_file)
             
             self.assertEqual(meta.name, meta2.name)
             self.assertEqual(meta.version, meta2.version)
             self.assertEqual(meta.description, meta2.description)
             self.assertEqual(meta.depends, meta2.depends)
             self.assertEqual(meta.conflicts, meta2.conflicts)
             self.assertEqual(meta.replaces, meta2.replaces)
         finally:
             if os.path.exists(self.__meta_file): os.remove(self.__meta_file)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestMeataFileWriter)
    TextTestRunner(verbosity=2).run(suite) 
